package me.goldze.mvvmhabit.binding.viewadapter.textview

import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.text.Html
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.method.ScrollingMovementMethod
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import me.jessyan.autosize.utils.AutoSizeUtils

/**
 * Created by goldze on 2017/6/16.
 */

object ViewAdapter {
    @BindingAdapter(value = ["htmlText"])
    @JvmStatic
    fun setHTMLText(view: TextView, htmlText: String) {
        view.text = Html.fromHtml(htmlText)
    }

    @BindingAdapter(value = ["android:singleLine"])
    @JvmStatic
    fun setSingleLine(view: TextView, value: Boolean) {
        if (value) {
            view.setSingleLine()
            view.ellipsize = TextUtils.TruncateAt.valueOf("END")
        }
    }

    @BindingAdapter(value = ["android:maxLines"])
    @JvmStatic
    fun setMaxLength(view: TextView, value: Int) {
        view.maxLines = value
        view.ellipsize = TextUtils.TruncateAt.valueOf("END")
    }

    @BindingAdapter(value = ["font"])
    @JvmStatic
    fun setFontFromAssets(view: TextView, typeface: Typeface) {
        view.typeface = typeface
    }

    @BindingAdapter(value = ["scrollable"])
    @JvmStatic
    fun setFontFromAssets(view: TextView, scrollable: Boolean) {
        view.movementMethod = if (scrollable) ScrollingMovementMethod.getInstance() else null
    }


    @BindingAdapter(value = ["text", "font12Text", "font12Color"], requireAll = false)
    @JvmStatic
    fun setStatisticsText(view: TextView, text: String?, font12Text: String?, font12Color: Int = ContextCompat.getColor(view.context, com.wyj.qmui.R.color.qmui_config_color_light)) {
        val textValue = text ?: ""
        val font12TextValue = font12Text ?: ""
        val text = textValue + font12TextValue
        val sp = SpannableString(text)
        sp.setSpan(AbsoluteSizeSpan(AutoSizeUtils.dp2px(view.context, 12f)), textValue.length, text.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        sp.setSpan(ForegroundColorSpan(font12Color), textValue.length, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        view.text = sp
    }


    @BindingAdapter(value = ["titleBlockColor", "titleBlockHeight"], requireAll = true)
    @JvmStatic
    fun setTitleBlock(view: TextView, color: Int, height: String) {
        var viewHeight = -1
        if (height.endsWith("dp")) {
            height.replace("dp", "").toFloatOrNull()?.let {
                viewHeight = AutoSizeUtils.dp2px(view.context, it)
            }
        } else if (height.endsWith("px")) {
            viewHeight = height.replace("px", "").toIntOrNull() ?: -1
        }
        val drawable = ColorDrawable(color)
        val px4 = AutoSizeUtils.dp2px(view.context, 4f)
        drawable.setBounds(0, 0, px4, viewHeight)
        view.setCompoundDrawables(drawable, null, null, null)
        view.compoundDrawablePadding = px4 * 2
    }
}